﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Common;
using DBA.Bll;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Web.UI;
using DBA.Model;

namespace DBA.Web
{
    public partial class _default : System.Web.UI.Page
    {
        /// <summary>
        /// 左导航菜单
        /// </summary>
        public string LeftNavigationStr = String.Empty;
        public string UserName = String.Empty;
        public string roleName = String.Empty;
        public string DepartName = String.Empty;
        public string XiName = String.Empty;
        public string ServerDate = String.Empty;
        public string SuoName = String.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            UserName = ServerContext.AdminCurrentUser.UserName;
            DepartName = ServerContext.AdminCurrentUser.DptName;
            XiName = ServerContext.AdminCurrentUser.XiName;
            SuoName = ServerContext.AdminCurrentUser.SuoName;
            foreach(string item in ServerContext.AdminCurrentUser.RoleName)
            {
                roleName += "【" + item + "】,";
            }
            roleName = roleName.TrimEnd(',');
            if (!VerificationHelper.IsNullOrEmpty(ServerContext.AdminCurrentUser.UserFuncList))
            {
                LeftNavigationStr = GetUserRoleLeftNavigation();
                foreach (Sys_SystemFunction item in ServerContext.AdminCurrentUser.UserFuncList)
                {

                    Panel tempPanel = (Panel)module_list.FindControl("pan_" + item.FuncID.ToString());
                    if (!VerificationHelper.IsNullOrEmpty(tempPanel))
                    {
                        tempPanel.Visible = true;
                    }
                }
            }
        }

        private string GetUserRoleLeftNavigation()
        {
            StringBuffer result = new StringBuffer();

            var tmpList = from r in ServerContext.AdminCurrentUser.UserFuncList.AsEnumerable()
                          where r.ParentFuncID == 1
                          orderby r.FuncID ascending
                          select r;
            foreach (var item in tmpList)
            {
                result += "<div class=\"accordionHeader\"><h2><span>Folder</span>" + item.FuncName + "</h2></div>";
                //判断是否有子节点
                int childCount = ServerContext.AdminCurrentUser.UserFuncList.Where(m => m.ParentFuncID == item.FuncID).Count(); ;
                if (childCount > 0)
                {
                    result += "<div class=\"accordionContent\"><ul class=\"tree treeFolder\">";
                    GetUserRoleChildLeftNavigation(item.FuncID, result);
                    result += "</ul></div>";
                }
            }
            return result.ToString();
        }

        private void GetUserRoleChildLeftNavigation(int parentID, StringBuffer result)
        {
            var tmpList = from r in ServerContext.AdminCurrentUser.UserFuncList.AsEnumerable()
                          where r.ParentFuncID == parentID
                          orderby r.FuncID ascending
                          select r;
            int i = tmpList.Count();
            if (i > 0)
            {
                foreach (var item in tmpList)
                {
                    int j = ServerContext.AdminCurrentUser.UserFuncList.Where(m => m.ParentFuncID == item.FuncID).Count();
                    if (j > 0)
                    {
                        result += "<li><a href=\"javascript:;\">"+item.FuncName+"</a><ul>";
                        GetUserRoleChildLeftNavigation(item.FuncID, result);
                        result += "</ul></li>";
                    }
                    else
                    {
                        result += "<li><a href=\""+item.FuncUrl+"\" target=\"navTab\" rel=\"external1\" external=\"true\">"+item.FuncName+"</a></li>";
                    }
                }
            }
        }
    }
}